<?php

namespace app\admin\controller;


use app\common\controller\AdminController;
use app\common\services\annotation\ControllerAnnotation;
use app\common\services\annotation\NodeAnnotation;

use support\Request;
use think\facade\Db;
use think\Exception;
use support\Response; //请求类

/**
 * @ControllerAnnotation(title="user")
 */
class UserController extends AdminController
{


    public function initialize()
    {
        
        parent::initialize();
        $this->model = new \app\admin\model\User();
        //return $this->success('登录成功');
    }
    
    public function index(Request $request): Response
    {
        if (!$request->isAjax()) return $this->fetch('admin/user/index');
        if ($request->input('selectFields')) {
            return $this->selectList();
        }
        list($page, $limit, $where) = $this->buildTableParams();
        $count = $this->model->where($where)->count();
        $list  = $this->model->where($where)->order($this->order)->page($page, $limit)->select()->toArray();
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];
        return json($data);
    }
    
    public function uedit(Request $request){
        $arr = $request->get();
        if(count($arr) > 1){
            if(!$arr['pw'] or $arr['pw'] == NULL){
                unset($arr['pw']);
            }else{
                $arr['pw'] = md5($arr['pw']);
            }
            DB::name('user')->where(['id'=>$arr['id']])->update($arr);
            
            
            //互联部分
    		$uinfo = DB::name('user')->where(['id'=>$arr['id']])->find();
    		$返回 = Db::connect('d_taiwan')->table('accounts')->where(['accountname'=>$uinfo['name']])->count();

            if($返回 > 0){
                if(sysconfig('game','gamelogin') == '1'){
                    Db::connect('d_taiwan')->table('accounts')->where(['accountname'=>$uinfo['name']])->update(['password'=>md5($arr['pw'])]);
                    Db::connect('Rslogin')->table('accountInfo')->where(['accountname'=>$uinfo['name']])->update(['password'=>md5($arr['pw'])]);
                }elseif(sysconfig('game','gamelogin') == '2'){
                    b::connect('d_taiwan')->table('accounts')->where(['accountname'=>$uinfo['name']])->update(['password'=>md5($arr['pw'])]);
                }
            }
            
            
            
            return $this->success('保存成功');
            //$this->success('成功');
            //
        }else{
            $row = DB::name('user')->where(['id'=>$arr['id']])->find();
            $this->assign(compact('row'));
            return view('admin/user/edit');
        }
        
    }
}
